window.addEventListener('load', function () {
    const carousel = document.querySelector('.carousel')
    const carouselR = document.querySelector('.carousel_r')
    const carouselL = document.querySelector('.carousel_l')

    carousel.addEventListener('mouseenter', function () {
        carouselR.style.display = 'block'
        carouselL.style.display = 'block'
        clearInterval(timeId)
    })
    carousel.addEventListener('mouseleave', function () {
        carouselR.style.display = 'none'
        carouselL.style.display = 'none'
        timeId = setInterval(function () {
            carouselR.click()
        }, 1000)
    })
    const ul = document.querySelector('.carousel ul')
    const ol = document.querySelector('.carousel ol')

    for (let i = 0; i < ul.children.length; i++) {
        const newLi = document.createElement('li')
        newLi.dataset.index = i
        ol.appendChild(newLi)
    }
    ol.children[0].classList.add('active')

    //// 进行ul中头尾元素的增加
    const newLastLi = ul.children[ul.children.length - 1].cloneNode(true)
    const newFirstLi = ul.children[0].cloneNode(true)

    ul.appendChild(newFirstLi)
    ul.insertBefore(newLastLi, ul.children[0])

    let num = 1
    let circle = 0
    const itemWidth = carousel.offsetWidth
    let flag = true
    carouselR.addEventListener('click', function () {
        if (flag) {
            ul.style.transition = 'all 1s'
            num++
            circle++
            if (num > ul.children.length - 1) {
                num = 0
            }
            if (circle > ol.children.length - 1) {
                circle = 0
            }

            updateCircle()
            ul.style.left = -itemWidth * num + 'px'
            flag = false
            setTimeout(function () {
                flag = true
            }, 1000)
        }
    })
    carouselL.addEventListener('click', function () {
        if (flag) {
            ul.style.transition = 'all 1s'
            num--
            circle--
            if (num < 0) {
                num = ul.children.length - 1
            }
            if (circle < 0) {
                circle = ol.children.length - 1
            }
            updateCircle()
            ul.style.left = -itemWidth * num + 'px'
            flag = false
            setTimeout(function () {
                flag = true
            }, 1000)
        }
    })

    ol.addEventListener('click', function (e) {
        if (e.target.tagName === 'LI') {
            circle = +e.target.dataset.index
            num = circle + 1
            updateCircle()
            ul.style.left = -itemWidth * num + 'px'
        }
    })

    function updateCircle() {
        document.querySelector('ol>li.active').classList.remove('active')
        ol.children[circle].classList.add('active')
    }

    let timeId = setInterval(function () {
        carouselR.click()
    }, 1000)

    ul.addEventListener('transitionend', function () {
        // 判断是不是最后一张的过渡结束, 如果是,就偷梁换柱
        if (num === ul.children.length - 1) {
            num = 1
            circle = 0
            ul.style.transition = 'none'
            ul.style.left = -itemWidth * num + 'px'
        }

        if (num === 0) {
            num = ul.children.length - 2
            circle = ol.children.length - 1
            ul.style.transition = 'none'
            ul.style.left = -itemWidth * num + 'px'

        }
    })

})
const more = document.querySelector('.assortGoods .more')
more.addEventListener('mouseover', function () {
    console.log('aaa');
    more.style.color = '#00c39e'
})
more.addEventListener('mouseout', function () {
    more.style.color = '#999'
})
 shoppingsss(goodArr)

// let username = localStorage.getItem("uname")

// const li1 = document.querySelector(".xtx_navs li:nth-child(1)")
// const li2 = document.querySelector(".xtx_navs li:nth-child(2)")
// if (username) {
//     li1.innerHTML = `<a href="javascript:;"><i class="iconfont icon-user">${username}</i></a>`
//     li2.innerHTML = '<a href="javascript:;">退出登录</a>'
// } else {
//     li1.innerHTML = '<a href="../html/login.html">请先登录</a>'
//     li2.innerHTML = '<a href="../html/register.html">免费注册</a>'
// }
// // 退出登录
// li2.addEventListener("click", function () {
//     // 退出登录
//     //  删除localStroage的uuu的数据
//     localStorage.removeItem("uname")
//     location.reload(true)
// })
